package com.epalmpay.controller.channel.member;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.epalmpay.dto.memberGrade.*;
import com.epalmpay.entity.MemberFee;
import com.epalmpay.service.member.IMemberFeeSettingService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.entity.MemberGrade;
import com.epalmpay.service.member.IMemberGradeService;
import com.epalmpay.service.member.IMemberService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.PropertyCopyUtil;

/** 
 * 说明：会员等级
 * 创建人：gwn
 * 创建时间：2017-05-9
 */
@Controller
@RequestMapping(value="/manager/member")
public class MemberGradeController  extends BaseController{
	
	@Resource
	private IMemberGradeService memberGradeService;
	
	@Resource
	private IMemberService memberService;
	
	@Resource
	private ISysLogService sysLogService;

	@Resource
	private IMemberFeeSettingService memberFeeSettingService;
	/**
	 * 会员等级列表
	 * @param memberGradeOut
	 * @return
	 */
	
	@RequestMapping(value="/listMembers")
	@ResponseBody
	public MemberGradeListOutDTO  listMember(MemberGradeInDTO memberGradeOut){
		
		MemberGradeListOutDTO memberListOutDTO=new MemberGradeListOutDTO();
		try {
			
			List<MemberGradeOutDTO> memberOutList=memberGradeService.listMembers(memberGradeOut);
			memberListOutDTO.setMemberOutList(memberOutList);
			memberListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberListOutDTO;
			
		} 
		catch (ApplicationException ae) { 
			memberListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 memberListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberListOutDTO;
	}

	/**
	 * 查询费率
	 * @param memberFeeSettingInDTO
	 * @return
	 */
	@RequestMapping(value="/queryMemberFee")
	@ResponseBody
	public MemberFeeSettingList queryMemberFee(MemberFeeSettingInDTO memberFeeSettingInDTO){
		MemberFeeSettingList memberFeeSettingList = new MemberFeeSettingList();
		try {
			List<MemberFeeSettingOutDTO> memberFeeSettingOutDTOList = memberFeeSettingService.queryMemberFee(memberFeeSettingInDTO);

			memberFeeSettingList.setMemberFeeSettingOutDTOList(memberFeeSettingOutDTOList);
			memberFeeSettingList.setErrorCode(Rescode.SUCCESS, "处理成功");

//			String content ="新增会员等级";
//			sysLogService.addSysLog(memberGradeInDTO.getUserId(),memberGradeInDTO.getUserName(),memberGradeInDTO.getIp(),content,memberGradeInDTO.getMerchantId());
			return memberFeeSettingList;
		} catch (ApplicationException ae) {
			memberFeeSettingList.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			memberFeeSettingList.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberFeeSettingList;
	}



	
	/**
	 * 会员等级的保存
	 */
	@RequestMapping(value="/saveMemberGrade")
	@ResponseBody
	public MemberGradeListOutDTO  saveMemberGrade(MemberGradeInDTO memberGradeInDTO){
		MemberGradeListOutDTO memberGradeListOutDTO = new MemberGradeListOutDTO();
		try { 
            //保存会员等级
			memberGradeService.saveMemberGrade(memberGradeInDTO);
			memberGradeListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			
			String content ="新增会员等级";
			sysLogService.addSysLog(memberGradeInDTO.getUserId(),memberGradeInDTO.getUserName(),memberGradeInDTO.getIp(),content,memberGradeInDTO.getMerchantId());
			return memberGradeListOutDTO;
		}catch (ApplicationException ae) {
			memberGradeListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 memberGradeListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberGradeListOutDTO;
	}		
	
	/**
	 * 查询会员等级否存在
	 * 
	 */
	@RequestMapping(value="/queryUpdateAmount")
	@ResponseBody
	public MemberGradeListOutDTO  queryUpdateaAmount(MemberGradeInDTO memberGradeOut){
		MemberGradeListOutDTO memberGradeListOutDTO = new MemberGradeListOutDTO();
		
		try {   
			MemberGradeOutDTO memberGradeOutDTO=memberGradeService.findAmountByUpdateAmount(memberGradeOut);
			memberGradeListOutDTO.setMemberGradeOutDTO(memberGradeOutDTO);
//			int count =memberGradeService.findAmountByUpdateAmount(memberGradeOut);

			return memberGradeListOutDTO;
			
		} catch (ApplicationException ae) {
			memberGradeListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			memberGradeListOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		memberGradeListOutDTO.setErrorCode("-2", "处理失败");
		return memberGradeListOutDTO;
	}
	/**
	 * 查询晋级金额否存在
	 *
	 */
	@RequestMapping(value="/queryUpdateAmount2")
	@ResponseBody
	public MemberGradeListOutDTO  queryUpdateaAmount2(MemberGradeInDTO memberGradeOut){
		MemberGradeListOutDTO memberGradeListOutDTO = new MemberGradeListOutDTO();

		try {
			MemberGradeOutDTO memberGradeOutDTO=memberGradeService.findAmountByUpdateAmount2(memberGradeOut);
			memberGradeListOutDTO.setMemberGradeOutDTO(memberGradeOutDTO);
//			int count =memberGradeService.findAmountByUpdateAmount(memberGradeOut);

			return memberGradeListOutDTO;

		}catch (ApplicationException ae) {
			memberGradeListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}catch (Exception e) {
			memberGradeListOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		memberGradeListOutDTO.setErrorCode("-2", "处理失败");
		return memberGradeListOutDTO;
	}

	
	
	
	/**
	 * 通过会员id查询会员等级详情
	 * 
	 */
	@RequestMapping(value="/findMemberGradeById")
	@ResponseBody
	public MemberGradeOutDTO  findMemberGradeById(MemberGradeInDTO memberGradeOut){
		MemberGradeOutDTO memberGradeOuts = new MemberGradeOutDTO();
		MemberGrade memberGrade=new MemberGrade();
		try {   
			memberGrade.setId(memberGradeOut.getId());
			MemberGrade MemberGrades=memberGradeService.findById(memberGrade);
			
			if(MemberGrades!=null){
				PropertyCopyUtil.copyPropertiesExclude(MemberGrades,memberGradeOuts);
				memberGradeOuts.setErrorCode(Rescode.SUCCESS, "查询成功");
				return memberGradeOuts;
			}else{
				memberGradeOuts.setErrorCode("-1", "未查询到记录");
			}
		} catch (ApplicationException ae) {
			memberGradeOuts.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}catch (Exception e) {
			memberGradeOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		memberGradeOuts.setErrorCode("-2", "处理失败");
		return memberGradeOuts;
	}
	
	/**
	 * 修改会员等级信息
	 * 
	 */
	@RequestMapping(value="/updateMemberGrade")
	@ResponseBody
	public MemberGradeOutDTO  updateMemberGrade(@RequestBody MemberGradeInDTO memberGradeOut){
		MemberGradeOutDTO memberGradeOuts=new MemberGradeOutDTO();
		MemberGrade memberGrade = new MemberGrade();
		try {   
			memberGrade.setId(memberGradeOut.getId());
			MemberGrade memberGrades=memberGradeService.findById(memberGrade);
			memberGrades.setName(memberGradeOut.getName());
			
//			memberGrades.setGradeRateId(memberGradeOut.getGradeRateId());//走的是支付平台
			memberGrades.setMemberFeeId(memberGradeOut.getMemberFeeId());
			memberGrades.setIcon(memberGradeOut.getIcon());
			memberGrades.setDiscountRate(memberGradeOut.getDiscountRate());
			memberGrades.setUpdateAmount(memberGradeOut.getUpdateAmount());
			if(memberGradeOut.getAttach1() !=null){
				int count = Integer.parseInt(memberGradeOut.getAttach1()) * 100;
				memberGrades.setAttach1(String.valueOf(count));
			}
//			memberGrades.setAttach1(memberGradeOut.getAttach1());
			memberGrades.setCanDevelopeMember(memberGradeOut.getCanDevelopeMember());
//			memberGrades.setWalletCanConsume(memberGradeOut.getWalletCanConsume());
			memberGrades.setWalletCanConsume(true);
			memberGrades.setWalletCanReceive(memberGradeOut.getWalletCanReceive());
			memberGrades.setWalletCanRecharge(memberGradeOut.getWalletCanRecharge());
			memberGrades.setWalletCanTransfer(memberGradeOut.getWalletCanTransfer());
			memberGrades.setCanWithdraw(memberGradeOut.getCanWithdraw());
			memberGrades.setNeedAudit(memberGradeOut.getNeedAudit());
			memberGradeService.editMemberGrade(memberGrades);
			memberGradeOuts.setErrorCode(Rescode.SUCCESS, "修改成功");
			return memberGradeOuts;
		} catch (Exception e) {
			memberGradeOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		memberGradeOuts.setErrorCode("-2", "处理失败");
		return null;
	}
	
	
	/**
	 * 删除会员等级信息
	 */
/*	@RequestMapping(value="/deleteMemberGrade")
	@ResponseBody
	public MemberGradeOutDTO  deleteMemberGrade(MemberGradeInDTO memberGradeInDTO){
		MemberGradeOutDTO memberGradeOutDTO=new MemberGradeOutDTO();
		MemberGrade memberGrade = new MemberGrade();
		try {  
			memberGrade.setId(memberGradeInDTO.getId());
			memberGradeService.deleteMemberGrade(memberGrade); 
			memberGradeOutDTO.setErrorCode(Rescode.SUCCESS, "删除用户信息成功");
			return memberGradeOutDTO;
		} catch (Exception e) { 
			memberGradeOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	*/
	
	/**
	 * 通过会员等级id查询会员等级详情信息
	 * 
	 */
	@RequestMapping(value="/queryMemberGradeById")
	@ResponseBody
	public MemberGradeOutDTO  queryMemberGradeById(MemberGradeInDTO memberGradeOut){
		MemberGradeOutDTO memberGradeOuts=new MemberGradeOutDTO();
		MemberGrade memberGrade = new MemberGrade();
		try {   
			memberGrade.setId(memberGradeOut.getId());
			MemberGrade memberGrades=memberGradeService.queryMemberGradeById(memberGrade);
			if(memberGrades!=null){
				PropertyCopyUtil.copyPropertiesExclude(memberGrades,memberGradeOuts,"page");
				memberGradeOuts.setErrorCode(Rescode.SUCCESS, "查询成功");
				return memberGradeOuts;
			}else{
				memberGradeOuts.setErrorCode("-1", "未查询到记录");
			}
		} catch (Exception e) { 
			memberGradeOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		memberGradeOuts.setErrorCode("-2", "处理失败");
		return memberGradeOuts;
	}
	
	@RequestMapping("/getAllMemberGradeIdAndName")
	@ResponseBody
	public List<Map> getAllMemberGradeIdAndName(@RequestBody Long groupId) throws Exception {
		return memberGradeService.getAllMemberGradeIdAndName(groupId);
	}


	@RequestMapping(value="/queryFees")
	@ResponseBody
	public MemberGradeListOutDTO  queryFees(MemberGradeInDTO memberGradeOut){

		MemberGradeListOutDTO memberGradeListOutDTO=new MemberGradeListOutDTO();
		try {

			List<MemberFee> memberFeeList=memberGradeService.queryFees(memberGradeOut);
			memberGradeListOutDTO.setMemberFeeList(memberFeeList);
			memberGradeListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberGradeListOutDTO;

		}
		catch (ApplicationException ae) {
			memberGradeListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			memberGradeListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberGradeListOutDTO;
	}

	@RequestMapping(value="/memberGrades")
	@ResponseBody
	public MemberGradeListOutDTO  memberGrades(MemberGradeInDTO memberGradeInDTO){

		MemberGradeListOutDTO memberGradeListOutDTO=new MemberGradeListOutDTO();
		try {

			List<MemberGrade> memberGradeList=memberGradeService.memberGrades(memberGradeInDTO);
			memberGradeListOutDTO.setMemberGrades(memberGradeList);
			memberGradeListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return memberGradeListOutDTO;

		}
		catch (ApplicationException ae) {
			memberGradeListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		catch (Exception e) {
			memberGradeListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return memberGradeListOutDTO;
	}




}
